package com.bcgtgjyb.huanwen.customview.mylibrary.tool;

import com.bcgtgjyb.huanwen.customview.mylibrary.DataAnimatorValue;

import ohos.agp.utils.RectFloat;

/**
 * Move 类
 *
 * @since 2016-02-29
 */
public class Move {
    // 默认1秒
    private static final int TIME = 1000;

    // 被除数
    private static final int TWO = 2;

    // 角度
    private static final double DEGREE = 360d;
    private static final int CIRCLEDEGREE = 360;
    private String TAG = Move.class.getName();
    private DataAnimatorValue vAX = null;
    private DataAnimatorValue vAY = null;
    private DataAnimatorValue circleVA = null;
    /**
     * 定义bwCallback变量
     */
    public BWCallback bwCallback;

    /**
     * Move 构造方法
     *
     */
    public Move() {
    }

    /**
     * RectFloat 方法
     *
     * @param rectF 矩形
     * @param xx x
     * @param yy y
     * @return 0
     */
    public RectFloat xy(RectFloat rectF, int xx, int yy) {
        return xy(rectF, xx, yy, TIME);
    }

    /**
     * xy 方法
     *
     * @param coodinateX 坐标X
     * @param coodinateY 坐标y
     * @param xyx x
     * @param yx y
     * @param time 时间
     * @return 0
     */
    public float[] xy(float coodinateX, float coodinateY, int xyx, int yx, int time) {
        if (vAX == null) {
            vAX = BWValueAnimator.getValueAnimator(xyx, time);
            vAX.start();
        }
        if (vAY == null) {
            vAY = BWValueAnimator.getValueAnimator(yx, time);
            vAY.start();
        }
        if (!vAX.isRunning()) {
            float[] endXY = new float[TWO];
            endXY[0] = coodinateX + xyx;
            endXY[1] = coodinateY + yx;
            vAX = null;
            vAY = null;
            if (bwCallback != null) {
                bwCallback.onFinish();
            }
            return endXY;
        }
        float xx = (float) vAX.getAnimatedValue();
        float yy = (float) vAY.getAnimatedValue();
        float[] xy = new float[2];
        xy[0] = coodinateX + xx;
        xy[1] = coodinateY + yy;
        return xy;
    }

    /**
     * xy 方法
     *
     * @param rectF 矩形F
     * @param xy x
     * @param yx y
     * @param time 时间
     * @return 0
     */
    public RectFloat xy(RectFloat rectF, int xy, int yx, int time) {
        if (vAX == null) {
            vAX = BWValueAnimator.getValueAnimator(xy, time);
            vAX.start();
        }
        if (vAY == null) {
            vAY = BWValueAnimator.getValueAnimator(yx, time);
            vAY.start();
        }
        if (!vAX.isRunning()) {
            RectFloat ff = new RectFloat(0, 0, 0, 0);
            if (bwCallback != null) {
                bwCallback.onFinish(circleVA);
            }
            return ff;
        }
        float xx = (float) vAX.getAnimatedValue();
        float yy = (float) vAY.getAnimatedValue();
        rectF.left = rectF.left + xx;
        rectF.right = rectF.right + xx;
        rectF.top = rectF.top + yy;
        rectF.bottom = rectF.bottom + yy;
        return rectF;
    }
    /**
     * circleMove 方法
     *
     * @param radioX x轴
     * @param radioY y轴
     * @param x x
     * @param y y
     * @param time 时间
     * @param degree 程度
     * @return 0
     */

    public float[] circleMove(int radioX, int radioY, int x, int y, int time, int degree) {
        float rr = (float) Math.sqrt((radioX - x) * (radioX - x) + (radioY - y) * (radioY - y));
        float nowDegree = (float) ((float) Math.atan((double) (y - radioY) / (x - radioX)) * DEGREE);
        if (circleVA == null) {
            circleVA = BWValueAnimator.getValueAnimator(degree, time);
        }
        if (!circleVA.isRunning()) {
            float[] end = new float[TWO];
            float endDegree = nowDegree + degree;
            end[0] = (float) (rr * Math.sin(endDegree / CIRCLEDEGREE));
            end[1] = (float) (rr * Math.cos(endDegree / CIRCLEDEGREE));
            if (bwCallback != null) {
                bwCallback.onFinish(circleVA);
            }
            return end;
        } else {
            float[] end = new float[TWO];
            float endDegree = (float) circleVA.getAnimatedValue() + degree;
            end[0] = (float) (rr * Math.sin(endDegree / CIRCLEDEGREE));
            end[1] = (float) (rr * Math.cos(endDegree / CIRCLEDEGREE));
            return end;
        }
    }

    public void setCallback(BWCallback callback) {
        this.bwCallback = callback;
    }

    /**
     * isRunning 方法
     *
     * @return 0
     */
    public boolean isRunning() {
        if (circleVA != null && circleVA.isRunning()) {
            return true;
        }
        if (vAX != null && vAX.isRunning()) {
            return true;
        }
        if (vAY != null && vAY.isRunning()) {
            return true;
        }
        return false;
    }

    /**
     * 清楚的方法
     *
     */
    public void clear() {
        circleVA = null;
        vAY = null;
        vAX = null;
    }
}
